#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <unistd.h>
#include <signal.h> 
#define MAX_LEN  20
#define SSIDLEN  10
void init_time();
void init_sigaction(void);
void prompt_info(int signo);
long double ave(long int* a, int len);
typedef struct
{
    char ssid[SSIDLEN];
    int command;//0:干扰，具体SSID见ssid[]，2：抓2G ， 5：抓5G
}command;
int str_to_number(const char* str);

int main(int argc,char*argv [])
{
    int sk;
	int send_flag;
    char buf[MAX_LEN];
    long int record[10] = { 0 };
    int index = 0;
    double tmp = 0;
    struct sockaddr_in ser_addr;                                //是用于指定对方(目的主机)信息
    struct sockaddr_in loc_addr;                                //可以用来指定一些本地的信息，比如指定端口进行通信，而不是让系统随机分配
    int ser_addr_len, loc_addr_len;
    int ret, count;
    struct in_addr addr;

    /* use for storage time */
    struct timeval start, end;
  


    //配置服务器信息
    bzero(&ser_addr, sizeof(ser_addr));
    ser_addr.sin_family = AF_INET;                                //设置为IPV4通信
    ser_addr.sin_addr.s_addr = inet_addr(argv[1]); //htonl(INADDR_ANY); //inet_addr("192.168.153.145");              //设置目的ip
    ser_addr.sin_port = htons(5050);            //设置目的端口去链接服务器
    ser_addr_len = sizeof(ser_addr);

    sk = socket(AF_INET, SOCK_DGRAM, 0);  
    if (sk < 0)
    {
        printf("socket create failure\n");
        return -1;
    }


    command com1[5];
    com1[0].command = 0;
    strcpy(com1[0].ssid, "zwr");
    memcpy(buf, (char*)&com1[0], sizeof(command));

	
    printf("开始发包测试\n");
    for (;;)
    {
        send_flag = sendto(sk, buf, sizeof(buf), 0, (struct sockaddr*)&ser_addr, ser_addr_len);
		while(send_flag == -1){
			printf("Client send failed\n");
			send_flag = sendto(sk, buf, sizeof(buf), 0, (struct sockaddr*)&ser_addr, ser_addr_len);
		}
        gettimeofday(&start, NULL);
        count = recvfrom(sk, buf, sizeof(buf), 0, (struct sockaddr*)&ser_addr, &ser_addr_len);
        if (count == -1){
            printf("receive data failure\n");
            continue;
        }
        gettimeofday(&end, NULL);
        /*send_flag = sendto(sk, buf, sizeof(buf), 0, (struct sockaddr*)&ser_addr, ser_addr_len);
		while(send_flag == -1){
		send_flag = sendto(sk, buf, sizeof(buf), 0, (struct sockaddr*)&ser_addr, ser_addr_len);
		}*/
        tmp = end.tv_sec * 1000.0 + end.tv_usec/1000.0 - (start.tv_sec * 1000.0 + start.tv_usec / 1000.0);
        /*if (index <= 9){ 
            record[index++] = tmp; }
        else{ 
            printf("每10组数据取一次时延的平均值为：%Lf ms\n",ave(record, 10));
            index = 0;
            record[index++] = tmp;
        }*/
  
        printf("Client收发一个包的单向时延: %lf ms\n\n",  tmp/2.0); // us
        //addr.s_addr = ser_addr.sin_addr.s_addr;
        //printf("Receive info: %s from %s %d\n", buf, inet_ntoa(addr), ser_addr.sin_port);
        sleep(1);

    }

    printf("communicate end\n");
    close(sk);
    return 0;
}
long double ave(long int* a, int len) {
    long int res = 0;
    for (int i = 0; i < len; i++) {
        res += a[i];
    }
    return res / 20000.0;
}

void prompt_info(int signo)
{
	
}

void init_sigaction(void)
{
	struct sigaction tact;
	/*信号到了要执行的任务处理函数为prompt_info*/
	tact.sa_handler = prompt_info;
	tact.sa_flags = 0;
	/*初始化信号集*/
	sigemptyset(&tact.sa_mask);
	/*建立信号处理机制*/
	sigaction(SIGALRM, &tact, NULL);
}

void init_time()
{
	struct itimerval value;
	/*设定执行任务的时间间隔为2秒0微秒*/
	value.it_value.tv_sec = 2;
	value.it_value.tv_usec = 0;
	/*设定初始时间计数也为2秒0微秒*/
	value.it_interval = value.it_value;
	/*设置计时器ITIMER_REAL*/
	setitimer(ITIMER_REAL, &value, NULL);
}

